import UIKit
import AssetsLibrary

class Album: NSObject {
    // 专辑名
    lazy var name: String = {
        if let group = self.group {
            return String(group.valueForProperty(ALAssetsGroupPropertyName))
        }
        return ""
    }()
    
    // 专辑类型
    lazy var type: UInt32 = {
        if let group = self.group {
            let type = String(group.valueForProperty(ALAssetsGroupPropertyType))
           return UInt32(type) ?? 0
        }
        return 0
    }()
    // 持久性ID
    lazy var persistentID: String = {
        if let group = self.group {
            return String(group.valueForProperty(ALAssetsGroupPropertyPersistentID))
        }
        return ""
    }()
    
    // 专辑url
    lazy var url: String = {
        if let group = self.group {
            return String(group.valueForProperty(ALAssetsGroupPropertyURL))
        }
        return ""
    }()
    
    // 默认Cover Photo
    lazy var defaultAlbumImage: UIImage = {
        if self.images.count > 0 {
            return self.images.last!.thumbnail!
        }
        
        return UIImage(named: "defaultAlbumImage")!
    }()
    
    var images: [AlbumImage] = []
    
    var group: ALAssetsGroup?
    init(group: ALAssetsGroup) {
        super.init()
        
        self.group = group
        weak var weakSelf = self
        group.enumerateAssetsUsingBlock { (asset, index, stop) in
            if let asset = asset {
                let albumImage = AlbumImage(asset: asset)
                weakSelf?.images.append(albumImage)
            }
        }
        
        // print("Album:name:\(name), type:\(type), persistentID:\(persistentID), url:\(url)\n")
    }
    
    deinit {
        // print("Album -- deinit: \(name)")
    }
}
